package xin.nick.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import xin.nick.config.SystemConstants;
import xin.nick.service.TaskService;
import xin.nick.domain.dto.TaskUpdateDTO;
import xin.nick.domain.dto.TaskCreateDTO;
import xin.nick.domain.query.TaskQuery;
import xin.nick.domain.vo.TaskVO;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 任务 前端控制器
 * </p>
 *
 * @author Nick
 * @since 2023-06-22
 */
@Tag(name = "任务模块")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/task")
public class TaskController {

    private final TaskService taskService;

    @Operation(summary = "任务详情",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @GetMapping("/getDetail/{id}")
    @PreAuthorize("@webSecurity.check('task:detail')")
    public TaskVO getDetail(@PathVariable(name = "id") Long id) {
        return taskService.getDetail(id);
    }

    @Operation(summary = "任务列表",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @GetMapping("/getList")
    @PreAuthorize("@webSecurity.check('task:list')")
    public List<TaskVO> getList(TaskQuery query) {
        return taskService.getList(query);
    }

    @Operation(summary = "任务列表分页",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @GetMapping("/getListPage")
    @PreAuthorize("@webSecurity.check('task:list')")
    public Page<TaskVO> getListPage(TaskQuery query) {
        return taskService.getListPage(query);
    }

    @Operation(summary = "任务创建",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @PostMapping("/create")
    @PreAuthorize("@webSecurity.check('task:add')")
    public TaskVO create(@RequestBody @Valid TaskCreateDTO dto) {
        return taskService.createTask(dto);
    }

    @Operation(summary = "任务更新",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @PutMapping("/update")
    @PreAuthorize("@webSecurity.check('task:update')")
    public TaskVO update(@RequestBody @Valid TaskUpdateDTO dto) {
        return taskService.updateTask(dto);
    }

    @Operation(summary = "任务删除",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasRole('" + SystemConstants.ROOT + "') || hasAnyAuthority('task:delete')")
    public void delete(@PathVariable(name = "id") Long id) {
        taskService.deleteById(id);
    }

    @Operation(summary = "任务批量删除",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @DeleteMapping("/deleteByIdList/{idList}")
    @PreAuthorize("@webSecurity.check('task:delete')")
    public void delete(@PathVariable(name = "idList") List<Long> idList) {
        taskService.deleteByIdList(idList);
    }

}
